package com.leo.base_project.activity.database;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.leo.base_project.R;
import com.leo.base_project.activity.base.BaseActivityX;
import com.leo.base_project.entity.BluDeviceDao;
import com.leo.base_project.entity.UserDao;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 * 类名：DataBaseActivity
 * 功能：用来查看保存的所有数据
 * 创建时间: 2021/8/26 13:24
 * 创建者：Leo
 */
@SuppressLint("NonConstantResourceId")
public class DataBaseActivity extends BaseActivityX {

    @BindView(R.id.sp_database)
    Spinner spDatabase;
    @BindView(R.id.rv_database)
    RecyclerView rvDatabase;

    UserDao userDao;
    BluDeviceDao bluDeviceDao;

    String[] databases;
    UserDataAdapter userDataAdapter;
    BluDeviceDataAdapter bluDeviceDataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base);
        ButterKnife.bind(this);
        initDatabase();
        initRecycleView();
        initSpinner();
    }

    private void initDatabase() {
        userDao = daoSession.getUserDao();
        bluDeviceDao = daoSession.getBluDeviceDao();
        databases = new String[]{"用户", "蓝牙设备"};
    }

    private void initRecycleView() {
        userDataAdapter = new UserDataAdapter(userDao.loadAll());
        bluDeviceDataAdapter = new BluDeviceDataAdapter(bluDeviceDao.loadAll());
        rvDatabase.setAdapter(userDataAdapter);
        rvDatabase.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initSpinner() {
        spDatabase.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, databases));
        spDatabase.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                switch (position) {
                    case 0:
                        rvDatabase.setAdapter(userDataAdapter);
                        break;
                    case 1:
                        rvDatabase.setAdapter(bluDeviceDataAdapter);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                rvDatabase.setAdapter(userDataAdapter);
            }
        });
    }
}